package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dao.AppPrivilegeDao;
import com.vf.admin.client.service.AppMingXiZuanShiService;
import com.vf.admin.client.service.AppPrivilegeService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppPrivilege;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.client.common.ClientConstants.client_toke;

/**
 * 贵族充值  --黑哲
 */
@Controller
@RequestMapping("/app/nobility")
public class AppNobilityController extends ClientBaseController {


    @Autowired
    AppPrivilegeService appPrivilegeService;
    @Autowired
    AppPrivilegeDao appPrivilegeDao;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppMingXiZuanShiService appMingXiZuanShiService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 获取贵族等级列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result list(HttpServletRequest request) {
        Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(client_toke));
        //获取当前登录用户的user对象
        AppUser appUser = appUserService.get(userId);
        List<AppPrivilege> list = appPrivilegeDao.list();
        for (AppPrivilege privilege : list) {
            if (appUser.getIsguizu() == 1) {
                if (appUser.getNobleid() >= privilege.getId()) {
                    privilege.setIsKaitong(2);
                } else {
                    privilege.setIsKaitong(1);
                    BigDecimal subtract = privilege.getUpCondition().subtract(new BigDecimal(Double.valueOf(appUser.getChongzhimoney()).toString()));
                    privilege.setOverNext(subtract);
                }
            } else {
                privilege.setIsKaitong(1);

                BigDecimal subtract = privilege.getUpCondition().subtract(new BigDecimal(Double.valueOf(appUser.getChongzhimoney()).toString()));
                privilege.setOverNext(subtract);
            }
        }
        Map<String, Object> mm = new HashMap<>(16);
        mm.put("list", list);
        //是否是贵族 1是贵族  2不是贵族
        mm.put("isguizu", appUser.getIsguizu());
        return sucessJSON(mm);
    }

    /**
     * 贵族开通
     *
     * @param guizuid 贵族ID
     * @return
     */
    @RequestMapping("/noblesOpen")
    @ResponseBody
    public Result noblesOpen(Integer guizuid) {
        Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
        if (userId == null) return errorJSON("请登录");
        AppUser user = appUserService.get(userId);
        if (StringEx.isNull(guizuid)) {
            return this.errorJSON("贵族id不能为空");
        }
        AppPrivilege appPrivilege = appPrivilegeService.get(guizuid);
        //判断用户是否是贵族
        if (user.getIsguizu() == 1) {
            if (appPrivilege.getId() < user.getNobleid()) {
                return this.errorJSON("只能升级更高级的贵族");
            }
        }
        if (user.getDiamonds().compareTo(appPrivilege.getMoney()) < 0) {
            return errorJSON("余额不足，请充值");
        }
        //调用贵族开通
        boolean flag = appPrivilegeService.guizuKaitong(user, appPrivilege);
        if (flag) {
            return sucessJSON("成功");
        } else {
            return errorJSON("网络繁忙，请稍后重试");
        }
    }


}
